/**
 * Sale Application
 * @author helio
 */
// 填充图片的本地引用
Ext.BLANK_IMAGE_URL = 'lib/ext/resources/images/default/s.gif';
Ext.chart.Chart.CHART_URL = 'lib/ext/resources/charts.swf';

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

var SALE;  
if (!SALE) SALE = {};

if (!SALE.img) SALE.img = {};
SALE.img.STAR_URL = '/lib/app/icons/star.png';
SALE.img.AWARD_STAR_GOLD_URL = '/lib/app/icons/award_star_gold.png';
SALE.img.AWARD_STAR_SILVER_URL = '/lib/app/icons/award_star_silver.png';
SALE.img.AWARD_STAR_BRONZE_URL = '/lib/app/icons/award_star_bronze.png';
SALE.img.AWARD_STAR_TAG_URL = '/lib/app/icons/tag_red.png';

if (!SALE.App) SALE.App = {};
SALE.App = {
	loginType : 'remote',//local,
	initialize : function() {
		this.viewpoint.destroy();
		this.viewpoint = new Ext.ux.AirWindow({					
			title: '富管家销售管理平台',
			iconCls:'icon-window',
			maximizable: true,
			panel: {
				id: 'mainpage',
				xtype: 'mainpage'
			}
			
		});
		
		this.registerHandlers();
		this.showLoginInfo();
	},
	
	initLoginPage : function() {
		//取得初始信息。
		this.initializeConfigInfo();
		
		//生成登录页面。
		this.viewpoint = new Ext.ux.AirWindow({					
			title: '富管家销售管理平台',
			iconCls:'icon-window',
			gear: true,
			initialise: true,
			panel: {
				id: 'loginpage',
				xtype: 'loginpage'
			}
			
		});
		
		//设定用户自定义皮肤。
		this.checkAppTheme();
	},	
	
	registerHandlers: function(){
		var moveFunction = function(){
			nativeWindow.startMove()
		};
		
		document.getElementById("north").onmousedown = moveFunction;
	},
	
	showLoginInfo : function() {
		
		var infoItem = document.getElementById("name_menu");
		if(infoItem.hasChildNodes()) {
			infoItem.removeChild(infoItem.childNodes[0]);
		}
		var infoNode = document.createTextNode(this.userInfo.name);
		infoItem.appendChild(infoNode);
	},
	
	initializeConfigInfo : function() {		
		if(!this.configInfo) {
			this.configInfo = LocalXMLFileUtil.configFileUtil.initInfo();
			this.configInfo.url = SALE.Utils.config.checkServiceUrl(this.configInfo.url);
		}
	},
	
	checkAppTheme : function() {
		if(this.configInfo["theme"] && this.configInfo["theme"] != "blue") {
			SALE.Utils.changeTheme(this.configInfo["theme"]);
		}
	},
	
	checkAuth : function (callback, auth) {
		if(SALE.App.userInfo.authz && SALE.App.userInfo.authz.indexOf("*") > -1) {
			
			callback();
		} else if(SALE.App.userInfo.authz) {
			var authz = SALE.App.userInfo.authz.split(",");
			var pass = false;
			Ext.each(authz, function(item){
				if(item == auth) {
					pass =true;
					return false;
				}
			}); 
			
			if (pass && callback) {
				callback();
			} else {
				SALE.Utils.showMessageBox("没有权限！","权限制约");
			}
			
		} else {
			SALE.Utils.showMessageBox("没有权限！","权限制约");
		}
		
	},
	
	logout : function() {
		SALE.Action.logoutAction(function(){
			clearInterval(SALE.Action.interval);			
			window.location.reload();
		});		
	},
	
	addCenterTabPanel : function(titleText, tabPanel) {
		var w = Ext.getCmp(titleText);
		var centerPanel = Ext.getCmp("mainpage").centerPanel;
		if(w) {
			centerPanel.remove(w,true);
		}
	    
	    centerPanel.add({
	        title: titleText,
	        id: titleText,
	        margins: '0 0 0 5',
	        border: false,
	        layout: 'fit',
	        
	        items: tabPanel
	    });
	    
	    centerPanel.setActiveTab(titleText);
	},
	
	showOperateTabPanel : function(tabPanel, title) {
		var w = Ext.getCmp("operateTabPanel");
		var centerPanel = Ext.getCmp("mainpage").centerPanel;
	    if (w) {
	        centerPanel.remove(w,true);
	    }
	    
	    centerPanel.add({
	        title: !title ? '业务处理窗口' : title,
	        id: 'operateTabPanel',
	        closable: true,
			iconCls: 'icon-tab-console',
	        margins: '0 0 0 5',
	        border: false,
	        layout: 'fit',
	        
	        items: tabPanel
	    });
	    
	    centerPanel.setActiveTab("operateTabPanel");
	},
	
	showNextOperateTabPanel : function(tabPanel, title) {
		var w = Ext.getCmp("nextOperateTabPanel");
		var centerPanel = Ext.getCmp("mainpage").centerPanel;
		if(w) {
			centerPanel.remove(w,true);
		}
	    
	    centerPanel.add({
	        title: title,
	        id: 'nextOperateTabPanel',
	        closable: true,
	        //autoScroll: true,
	        iconCls: 'icon-tab-page',
	        margins: '0 0 0 5',
	        border: false,
	        layout: 'fit',
	        
	        items: tabPanel
	    });
	    
	    centerPanel.setActiveTab("nextOperateTabPanel");

	},
	
	showHome : function() {
		Ext.getCmp('mainpage').centerPanel.activate(0);		
	},
	
	showThemeChange : function() {
		Ext.getCmp('theme-change-tip').show();
	}
}